//
//  HomeViewController.m
//  MyMovie
//
//  Created by Weiwei on 16/2/24.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "HomeModel.h"//主页cell的数据model
#import "CityViewController.h"
#import "PosterView.h"
@interface HomeViewController (){
    
    UITableView* _homeTableView;//作为主页面的电影cell的承载
    NSMutableArray* _dataList;//作为cell数据的model数组
    PosterView* _posterView;//海报试图
}

@end

@implementation HomeViewController


//重写init方法，在viewC被创建的时候就给他的navigationItem和tabBarItem
-(id)init{
    if (self = [super init]) {
        
        
        self.navigationItem.title = @"主页";
        self.tabBarItem.image = [UIImage imageNamed:@"index_menu_home"];
        
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self _setLeftButton];
    [self _setRightButton];

    [self _loadData];

    [self _addTableView];
    
#pragma -mark添加封装好的海报试图
    [self _addPosterView];
    
}

-(void)_addPosterView{
    
    _posterView =[[PosterView alloc]initWithFrame:self.view.bounds];
    //_posterView.backgroundColor = [UIColor redColor];
    _posterView.hidden = YES;
#pragma -mark 传递数据
    _posterView.dataList = _dataList;//将控制器的dataList里的数据给了大海报试图的dataList存储
    
    
    [self.view addSubview:_posterView ];
    
    
    
}










#pragma -mark JSON数据解析
-(void)_loadData{
    
    //1.使用自己封装的数据解析类方法解析josn数据
    NSDictionary* rootDataDic = [DataParser jsonObjectFromFileName:@"home_header"];
    
    
    
#pragma -mark目标四 创建一个model，将数据存入model，每一个cell对应一个dic，dic对应一个model
    
    //（1）从根数据结构字典里取出movies数组
    NSArray* moviesArray = [rootDataDic objectForKey:@"movies"];
    
    //（3）创建一个数组（可变数组）用于存放每一个cell的model
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
    
    //(2)for in循环，将数组中每一个电影dic的数据转存到model里
    for (NSDictionary* dic in moviesArray) {
        
#pragma -mark 使用封装好的方法
        HomeModel* model = [[HomeModel alloc]initWithDic:dic];
        
        //将model存入到一个数组中,方便tableView一会使用
        [_dataList addObject:model];
        
    }
    
    //数据源有了，用数据源设置tableView的代理方法了，我们该给cell传递数据
    
  
    
}














#pragma -mark创建一个tableView
-(void)_addTableView{
    
    //1.初始化全局的tableView
//    _homeTableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _homeTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH-64) style:UITableViewStylePlain];
    
    //设置背景颜色
    //_homeTableView.backgroundColor = [UIColor redColor];
    _homeTableView.backgroundColor = [UIColor clearColor];
    
    //2.加载到self.view上
    [self.view addSubview:_homeTableView];
    
    //3.设置代理方法
    _homeTableView.dataSource = self;
    _homeTableView.delegate = self;
    
}
//dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataList.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString* identifier = @"home_cell";
//    //复习一下
//    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
//    return cell;
    
#pragma -mark目标二 封装定制cell，从xib文件加载
    static NSString* identifier = @"home_cell";
    //1.从复用池中查找cell
    HomeTableViewCell* cell  = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil]lastObject];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
#pragma -mark 用model提供数据
   //利用model给cell传递数据
    //(1)取出cell相对应的model
    HomeModel* model = [_dataList objectAtIndex:indexPath.row];
    //(2)将model给cell传进去
    //首先cell添加一个model属性，重写set方法（set中对各个属性负值），把model给我们的cell
    cell.homeModel = model;
    
    return cell;
}

//发现单元格太小，设置cell的高度
//delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

//增加左Button城市按钮
-(void)_setLeftButton{
    
    //----------------------------------
    UIButton* leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [leftButton setTitle:@"北京" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //加载到导航item上
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

}
-(void)leftButtonAction:(UIButton*)button{
    
    //1.创建一个cityViewC
    CityViewController* cityViewController = [[CityViewController alloc]init];
    
    //但页面被PUSH弹出的时候,隐藏下面的tabbar
    [cityViewController setHidesBottomBarWhenPushed:YES];
    
#pragma -mark  调用对象带有block的方法
    [cityViewController chuanzhi:^(NSString *cityString) {
        [button setTitle:cityString forState:UIControlStateNormal];
        
    }];
    
    
    
    //2.导航控制器弹出一个控制器
    [self.navigationController pushViewController:cityViewController animated:YES];
    
    

}

#pragma -mark 实现右button的添加和旋转
-(void)_setRightButton{
    
    //原理：在一个view上有两个button，一个默认隐藏，另一个显示，将这个view最后添加到barbuttonitem上。当点击view（当钱button）会发生隐藏和旋转
    
    //1.创建一个view
    UIView* custemView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 49, 25)];
    //正面的button
    UIButton *button1 = [[UIButton alloc]initWithFrame:custemView.bounds];
    [button1 setBackgroundImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 10001;
    [custemView addSubview:button1];
    
    //反面的button
    UIButton *button2 = [[UIButton alloc]initWithFrame:custemView.bounds];
    [button2 setBackgroundImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 10002;
    button2.hidden = YES;
    [custemView addSubview:button2];
    
    //将custemView添加到导航栏上
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:custemView];
    
    
    
}
-(void)rightButtonAction:(UIButton*)button{
    
    //这个custemView也是我们翻转 的对象
    //1.获得custemView
     UIView* custemView = button.superview;
    
    if (button.tag == 10001) {
        button.hidden = YES;
        //取相反的button显示
        UIButton* otherButton = (UIButton*)[custemView viewWithTag:10002];
        otherButton.hidden = NO;
        
        
        //设置custemView的反转动画实现向左反转
        //过场动画和动画效果的嵌套
        [UIView animateWithDuration:.5 animations:^{
            
            [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:custemView cache:YES];
        }];
        
#pragma -mark实现self.view跟着反转
        [UIView animateWithDuration:.5 animations:^{
            
            [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
        }];

        //设置tableView隐藏
        _homeTableView.hidden = YES;
#pragma -mark 设置海报试图的显示
        _posterView.hidden = NO;
        
        
        
    }else{
        
        button.hidden = YES;
        //取相反的button显示
        UIButton* otherButton = (UIButton*)[custemView viewWithTag:10001];
        otherButton.hidden = NO;
        
        //设置custemView的反转动画实现向左反转
        //过场动画和动画效果的嵌套
        [UIView animateWithDuration:.5 animations:^{
            
            [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:custemView cache:YES];
        }];
        
        [UIView animateWithDuration:.5 animations:^{
            
            [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
        }];
        
        //设置tableView显示
        _homeTableView.hidden = NO;
        _posterView.hidden = YES;
        
        
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
